package com.mcmaster.android.menus;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.mcmaster.android.MarkLocation;
import com.mcmaster.android.R;

public class CourseSelectMenu extends MapActivity {
	
	//Courses database
	public String[ ][ ] locations = {
			{"Select a course","43.260928,-79.91933,McMaster University"},
	        {"SE 3A04","43.258886,-79.920602,Information Technology Building"},
	        {"SE 3GA3","43.260049,-79.918198,Health Sciences Centre"},
	        {"SE 3X03","43.260049,-79.918198,Health Sciences Centre"},
	        {"SE 3RA3","43.263112,-79.920194,Hamilton Hall"},
	        {"SE 3I03","43.258886,-79.920602,Information Technology Building"},
	        {"SE 3F03","43.260847,-79.920398,John Hodgins Engineering Building"},
	        {"SE 3BB4","43.258886,-79.920602,Information Technology Building"},
	        {"SE 3S03","43.258827,-79.919518,T13"},
	        {"SE 3DX4","43.263968,-79.916317,Degroote School of Business"},
	        {"ENG 4A03","43.260847,-79.920398,John Hodgins Engineering Building"},
	    };
	
	Spinner spinnerView;
	TextView display;
    MapView map;
    MapController controller, mc;
    List<Overlay> overlayList;
    Float startLocationX;
    Float startLocationY;
    Drawable d;
    OnTouch touch;
    long start, stop;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_select_layout);
        
        startLocationX = 43.260928f;
        startLocationY = -79.91933f;
        
        map = (MapView)findViewById(R.id.mapCourse);
        display = (TextView)findViewById(R.id.textView1);
        spinnerView = (Spinner) this.findViewById(R.id.sCourses);
        d = getResources().getDrawable(R.drawable.androidmarker);
        Button home = (Button)findViewById(R.id.bMM);
        
        home.setOnClickListener(new ButtonHomeHandler());
        
        map.setBuiltInZoomControls(true);
        
        touch = new OnTouch();
        overlayList = map.getOverlays();
        overlayList.add(touch);
        
        controller = map.getController();
        mc = map.getController();
        GeoPoint startLocation = new GeoPoint((int)(startLocationX * 1E6), (int)(startLocationY * 1E6)); 
        controller.animateTo(startLocation);
        controller.setZoom(17);
        
        //Spinner
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
        for(int i = 0; i < locations.length; i++)
            adapter.add(locations[i][0]);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinnerView.setAdapter(adapter);
        spinnerView.setOnItemSelectedListener(selectListener);
        gotoSelected();
	}
	
	private OnItemSelectedListener selectListener = new OnItemSelectedListener() {

	    public void onItemSelected(AdapterView<?>parent, View v, int position, long id){
	        gotoSelected();
	    }
	    public void onNothingSelected(AdapterView<?> arg0) {	
	    
	    }
	};
	
	private class ButtonHomeHandler implements View.OnClickListener {
	    public void onClick(View v) {
				handleHomeClick();
	    }
	}
	
	private void handleHomeClick() {
		finish();
	}

	public void gotoSelected(){

	    int index = spinnerView.getSelectedItemPosition();
	    String delims = "[,]";
	    String[] tokens = locations[index][1].split(delims);
	    Float lat = new Float (tokens[0]);
	    Float lng = new Float (tokens[1]);
	    Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
	    GeoPoint location = new GeoPoint ((int)(lat * 1E6), (int)(lng * 1E6));
        mc.animateTo(location);
        display.setText("" + tokens[2]);
        if(locations[index][0] != "Select a course"){
        	mc.setZoom(19);
        	OverlayItem markBuilding = new OverlayItem(location, "" + tokens[2], "");
            MarkLocation mark = new MarkLocation(d, CourseSelectMenu.this);
            overlayList.clear();
            overlayList.add(touch);
            mark.insertMarker(markBuilding);
            overlayList.add(mark);
        }
        else{
        	overlayList.clear();
            overlayList.add(touch);
        	mc.setZoom(17);
        }
	 }

	public boolean onKeyDown(int keyCode, KeyEvent event){
	    MapController mc = map.getController(); 
	    switch (keyCode) {
	        case KeyEvent.KEYCODE_2:
	            mc.zoomIn();
	            break;
	        case KeyEvent.KEYCODE_1:
	            mc.zoomOut();
	            break;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	class OnTouch extends Overlay{
		public boolean onTouchEvent(MotionEvent e, MapView m){
			if(e.getAction() == MotionEvent.ACTION_DOWN){
				start = e.getEventTime();
			}
			
			if(e.getAction() == MotionEvent.ACTION_UP){
				stop = e.getEventTime();
			}
			
			//If the user touches more than 500ms
			if(stop - start > 1200){
				AlertDialog toggle = new AlertDialog.Builder(CourseSelectMenu.this).create();
				//Standard View
				toggle.setButton("Standard View", new DialogInterface.OnClickListener() {					
					public void onClick(DialogInterface dialog, int which) {
						map.setSatellite(false);
						map.setStreetView(true);
					}
				});
				//Satellite View
				toggle.setButton2("Satellite View", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						map.setSatellite(true);
						map.setStreetView(false);
					}
				});
				toggle.show();
				return true;
			}
			return false;
		}
	}
}
